﻿

using IotContract.Dtos.Servers.Pushs;
using ProtoBuf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IotContract.Dtos.Servers.Process;
/// <summary>
/// 点位站台，默认带6个扫码枪
/// </summary>
public class CvUnitTagDto
{
    public string ProtobufType { get; set; }
    /// <summary>
    /// 点位站台
    /// </summary>
    public int TagNum { get; set; }
    /// <summary>
    /// 托盘号
    /// </summary>
    public string Tray1 { get; set; } = "无托盘";
    /// <summary>
    /// 托盘号
    /// </summary>
    public string Tray2 { get; set; } = "无托盘";
    /// <summary>
    /// 托盘号
    /// </summary>
    public string Tray3 { get; set; } = "无托盘";
    /// <summary>
    /// 托盘号
    /// </summary>
    public string Tray4 { get; set; } = "无托盘";
    /// <summary>
    /// 托盘号
    /// </summary>
    public string Tray5 { get; set; } = "无托盘";
    /// <summary>
    /// 托盘号
    /// </summary>
    public string Tray6 { get; set; } = "无托盘";
    /// <summary>
    /// 托盘号
    /// </summary>
    public string Tray1Old { get; set; }
    /// <summary>
    /// 托盘号
    /// </summary>
    public string Tray2Old { get; set; }
    /// <summary>
    /// 托盘号
    /// </summary>
    public string Tray3Old { get; set; }
    /// <summary>
    /// 托盘号
    /// </summary>
    public string Tray4Old { get; set; }
    /// <summary>
    /// 托盘号
    /// </summary>
    public string Tray5Old { get; set; }
    /// <summary>
    /// 托盘号
    /// </summary>
    public string Tray6Old { get; set; }
    /// <summary>
    /// 存在一层托盘
    /// </summary>
    public bool IsExistTray1 { get; set; }
    /// <summary>
    /// 存在二层托盘
    /// </summary>
    public bool IsExistTray2 { get; set; }
    /// <summary>
    /// 存在三层托盘
    /// </summary>
    public bool IsExistTray3 { get; set; }
    /// <summary>
    /// 存在四层托盘
    /// </summary>
    public bool IsExistTray4 { get; set; }
    /// <summary>
    /// 存在五层托盘
    /// </summary>
    public bool IsExistTray5 { get; set; }
    /// <summary>
    /// 存在六层托盘
    /// </summary>
    public bool IsExistTray6 { get; set; }

    /// <summary>
    /// 0:联机运行停止状态   1:联机运行启动状态,
    /// </summary>
    public int Active { get; set; }
    /// <summary>
    /// 重试次数
    /// </summary>
    public int RetryCount { get; set; }
    /// <summary>
    /// 重复提交
    /// </summary>
    public int BcrCommit { get; set; }
    /// <summary>
    /// 提交的Job
    /// </summary>

    public int BcrCommitJob { get; set; }
    /// <summary>
    /// 最初的jobNumber
    /// </summary>
    public int OriginJobNumber { get; set; }

    /// <summary>
    /// 扫码枪提交次数
    /// </summary>
    public int BcrCommitTimes { get; set; }
    /// <summary>
    /// 是否已经读取
    /// </summary>
    public bool IsReadBcr { get; set; }
    /// <summary>
    /// 次数
    /// </summary>
    public int Times { get; set; }
    /// <summary>
    /// 读取时间
    /// </summary>
    public DateTime? ReadTime { get; set; }

    /// <summary>
    /// 扫码的标志ID
    /// </summary>
    public string BcrID { get; set; }
    /// <summary>
    /// 扫码历史ID
    /// </summary>

    public string BcrOldID { get; set; }
    /// <summary>
    /// 目的地
    /// </summary>
    public int Dest { get; set; }
    /// <summary>
    /// 任务号
    /// </summary>
    public int JobNumber { get; set; }
    /// <summary>
    /// 任务类型
    /// </summary>
    public int JobType { get; set; }
    /// <summary>
    /// 写入的任务号
    /// </summary>
    public int WriteJobNumber { get; set; }
    /// <summary>
    /// 托盘类型
    /// </summary>
    public int TrayType { get; set; }
    /// <summary>
    /// 扫码枪准备
    /// </summary>
    public bool IsBcrReady { get; set; }
    /// <summary>
    /// 错误码
    /// </summary>
    public int ErrorCode { get; set; }
    /// <summary>
    /// 是否手动
    /// </summary>
    public bool IsManual { get; set; }
    /// <summary>
    /// 是否使用叠盘
    /// </summary>
    public bool UseMagazine { get; set; }
    /// <summary>
    /// 叠盘完成
    /// </summary>
    public bool MagazineOut { get; set; }
    /// <summary>
    /// 叠盘完成旧值
    /// </summary>
    public bool MagazineOutOld { get; set; }
    /// <summary>
    /// 设备号
    /// </summary>
    public string EquipNum { get; set; }
    /// <summary>
    /// 设备id
    /// </summary>
    public long EquipId { get; set; }
    /// <summary>
    /// 信息
    /// </summary>
    public string Message { get; set; }
    /// <summary>
    /// 托盘号
    /// </summary>
    public List<string> Trays { get; set; } = new List<string>();


    public bool UseBcr { get; set; }
    /// <summary>
    /// 最大的扫码枪
    /// </summary>
    public int MaxReadBcr { get; set; }
    /// <summary>
    /// 错误时候扫码数目
    /// </summary>
    public int ErrorMaxReadBcr { get; set; }
    /// <summary>
    /// 读取到的扫码枪
    /// </summary>
    public int ReadOkBcr { get; set; }

    /// <summary>
    /// 碟盘机排出时间
    /// </summary>
    public DateTime? StackOutTime { get; set; }
    /// <summary>
    /// 托盘停留时长（分钟）
    /// </summary>
    public double TrayOnTime { get; set; }
    /// <summary>
    /// 托盘进入时间
    /// </summary>
    public DateTime? TrayInTime { get; set; }

    /// <summary>
    /// 提交失败后的错误任务号
    /// </summary>
    public int ErrorJobNumber { get; set; }
    /// <summary>
    /// 关联的扫码枪
    /// </summary>
    public List<PushBcrDto> Bcrs { get; set; } = new List<PushBcrDto>();
    /// <summary>
    /// 站台包含的所有点位
    /// </summary>
    public List<PushEquipTag> Tags { get; set; } = new List<PushEquipTag>();


    #region 辅助判断，针对plc数据不稳定，容错属性
    /// <summary>
    /// 手动数据相同
    /// </summary>
    public int ManualSameCount { get; set; }
    #endregion

    public bool Connection { get; set; }
    /// <summary>
    /// 根据属性获取点位
    /// </summary>
    /// <param name="Property"></param>
    /// <returns></returns>
    public PushEquipTag GetTagByProperty(string Property)
    {
        var tag = Tags.FirstOrDefault(x => x.TagProperty == Property);
        if (tag == null)
        {
            return null;
        }
        else
        {
            return (PushEquipTag)tag.Clone();
        }
    }


    /// <summary>
    /// 是否是实时数据
    /// </summary>
    public bool IsRealTime()
    {
        if (ReadTime == null)
        {
            return false;
        }
        return DateTime.Now.Subtract(ReadTime.Value).TotalSeconds < 60;

    }
}

/// <summary>
/// 点位站台，带两个扫码枪
/// </summary>
public class F29CvUnitDto
{
    /// <summary>
    /// 入库站台允许取货
    /// </summary>
    public int AllowEnter { get; set; }
    /// <summary>
    /// 出库站台允许放货
    /// </summary>
    public int AllowOut { get; set; }

    /// <summary>
    /// 协议类型
    /// </summary>
    public string ProtobufType { get; set; }
    /// <summary>
    /// 点位站台
    /// </summary>
    public int TagNum { get; set; }
    /// <summary>
    /// 托盘号
    /// </summary>
    public string Tray1 { get; set; } = "无托盘";
    /// <summary>
    /// 托盘号
    /// </summary>
    public string Tray2 { get; set; } = "无托盘";
  
    /// <summary>
    /// 托盘号
    /// </summary>
    public string Tray1Old { get; set; }
    /// <summary>
    /// 托盘号
    /// </summary>
    public string Tray2Old { get; set; }
    /// <summary>
  
    /// <summary>
    /// 存在一层托盘
    /// </summary>
    public bool IsExistTray1 { get; set; }
    /// <summary>
    /// 存在二层托盘
    /// </summary>
    public bool IsExistTray2 { get; set; }
   

    /// <summary>
    /// 0:手动   1:自动
    /// </summary>
    public int Mode { get; set; }

    /// <summary>
    /// 叠盘时长
    /// </summary>
    public int StackOnTime { get; set; }

    /// <summary>
    /// 正在提交中
    /// </summary>
    public bool Commiting{ get; set; }

    /// <summary>
    /// 提交后返回的任务号
    /// </summary>
    public int CommitReturnTask { get; set; }
    /// <summary>
    /// 提交的任务号
    /// </summary>
    public int CommitTask { get; set; }

   
   
   
    /// <summary>
    /// 扫码枪读取时间
    /// </summary>
    public DateTime? BcrReadTime { get; set; }

    /// <summary>
    /// 扫码的标志ID
    /// </summary>
    public string BcrReadID { get; set; }
   
    /// <summary>
    /// 目的地
    /// </summary>
    public int Dest { get; set; }
    /// <summary>
    /// 任务号
    /// </summary>
    public int TaskNumber { get; set; }
   
    
    /// <summary>
    /// 托盘类型
    /// </summary>
    public int TrayType { get; set; }
    /// <summary>
    /// 写入完成
    /// </summary>
    public int WriteDone { get; set; }
    /// <summary>
    /// 扫码枪准备扫码
    /// </summary>
    public int BcrReady { get; set; }
    /// <summary>
    /// 错误码
    /// </summary>
    public int Error { get; set; }
   
   
   
    /// <summary>
    /// 叠盘完成
    /// </summary>
    public int StackOver { get; set; }

    /// <summary>
    /// 设备号
    /// </summary>
    public string EquipNum { get; set; }
    /// <summary>
    /// 设备id
    /// </summary>
    public long EquipId { get; set; }
    /// <summary>
    /// 信息
    /// </summary>
    public string Message { get; set; }
    /// <summary>
    /// 托盘号
    /// </summary>
    public List<string> Trays { get; set; } = new List<string>();


   
    /// <summary>
    /// 最大的扫码枪
    /// </summary>
    public int MaxReadBcr { get; set; }
 
    /// <summary>
    /// 读取到的扫码枪
    /// </summary>
    public int ReadOkBcr { get; set; }

    /// <summary>
    /// 碟盘机排出时间
    /// </summary>
    public DateTime? StackOutTime { get; set; }
    /// <summary>
    /// 托盘停留时长（分钟）
    /// </summary>
    public double TrayOnTime { get; set; }
    /// <summary>
    /// 托盘进入时间
    /// </summary>
    public DateTime? TrayInTime { get; set; }

   
    /// <summary>
    /// 关联的扫码枪
    /// </summary>
    public List<PushBcrDto> Bcrs { get; set; } = new List<PushBcrDto>();
    /// <summary>
    /// 站台包含的所有点位
    /// </summary>
    public List<PushEquipTag> Tags { get; set; } = new List<PushEquipTag>();


   
   

    public bool Connection { get; set; }
    /// <summary>
    /// 根据属性获取点位
    /// </summary>
    /// <param name="Property"></param>
    /// <returns></returns>
    public PushEquipTag GetTagByProperty(string Property)
    {
        var tag = Tags.FirstOrDefault(x => x.TagProperty == Property);
        if (tag == null)
        {
            return null;
        }
        else
        {
            return (PushEquipTag)tag.Clone();
        }
    }


    
}
